<?php

namespace App\Admin\Controllers\Vote\Season;

use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Actions\Season\ReplicateSeason;
use App\Admin\Actions\Season\SeasonDomainConfig;
use App\Admin\Traits\WithUploads;
use App\Models\Season;
use App\Models\Wechat;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class SeasonController extends AdminController
{
    use WithUploads;

    protected string $title = '赛季管理';

    public function grid(): Grid
    {
        $model = Season::ofAdmin()
            ->with(['wechat'])
            ->withCount(['chapters', 'players', 'gifts', 'sponsors', 'designs'])
            ->latest();

        return Grid::make($model, function (Grid $grid) {
            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
                $grid->disableCreateButton();
                $grid->disableActions();
            }
            $grid->actions([new ReplicateSeason, new SeasonDomainConfig]);
            $grid->batchActions([
                new BatchEnable(Season::class),
                new BatchDisable(Season::class),
            ]);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title', '赛季名称');
            });

            $grid->quickSearch(['title'])
                ->placeholder('赛季名称');

            $grid->column('title', '赛季标题');
            $grid->column('wechat.name', '微信公众号');
            $grid->column('cover')
                ->appThumb();
            $grid->column('赛季收入')
                ->display(fn() => $this->supports()->sum('amount'));
            $grid->column('chapters_count', '场次')
                ->link(fn() => route('admin.chapters.index', ['season_id' => $this->id]), '_self');
            $grid->column('players_count', '选手')
                ->link(fn() => route('admin.players.index', ['season_id' => $this->id]), '_self');
            $grid->column('gifts_count', '礼物')
                ->link(fn() => route('admin.seasons.gifts.index', $this), '_self');
            $grid->column('designs_count', '海报设计')
                ->link(fn() => route('admin.seasons.designs.index', $this), '_self');

            $grid->column('status')
                ->bool();
            $grid->column('created_at');
        });
    }

    public function form(): Form
    {
        return Form::make(Season::ofAdmin(), function (Form $form) {
            $form->block(7, function (Form\BlockForm $form) {
                $form->title('基础信息');
                $form->showFooter();
                $form->text('title', '赛季标题')
                    ->required();
                $form->switch('status')
                    ->default(true);
                $form->editor('content', '赛季说明')
                    ->imageUrl(route('admin.upload.editor'));

                $form->next(function (Form\BlockForm $form) {
                    $form->title('图片配置');
                    $this->cover($form)
                        ->dimensions(['min_width' => 375, 'max_width' => 750])
                        ->help('图片宽度请在375~750px之间');
                    $this->pictures($form)
                        ->dimensions(['min_width' => 375, 'max_width' => 750])
                        ->help('图片宽度请在375~750px之间');
                    $this->file($form, 'audio', '背景音乐');
                });
                $form->next(function (Form\BlockForm $form) {
                    $form->title('防刷配置');

                    $form->number('config.robot_day_max', 'IP、机型限制')
                        ->width(8, 4)
                        ->default(30)
                        ->help('每天同一【IP、机型】投票N次直接判定为刷票')
                        ->required();
                    $form->number('config.robot_day_max_ip', 'IP限制')
                        ->width(8, 4)
                        ->default(100)
                        ->help('每天同一【IP】投票N次直接判定为刷票')
                        ->required();
                    $form->number('config.like_period', '点赞检测周期')
                        ->width(8, 4)
                        ->default(1)
                        ->help('单位分钟，N分钟刷多少次 => 封禁点赞')
                        ->required();
                    $form->number('config.like_period_times', '点赞检测阀值')
                        ->width(8, 4)
                        ->default(60)
                        ->help('单位分钟，N分钟刷多少次 => 封禁点赞')
                        ->required();
                    $form->number('config.comment_period', '评论检测周期')
                        ->width(8, 4)
                        ->default(1)
                        ->help('单位分钟，N分钟刷多少次 => 封禁评论')
                        ->required();
                    $form->number('config.comment_period_times', '评论检测阀值')
                        ->width(8, 4)
                        ->default(60)
                        ->help('单位分钟，N分钟刷多少次 => 封禁票评论')
                        ->required();
                });
            });

            $form->block(5, function (Form\BlockForm $form) {
                $form->title('微信公众号配置');
                $form->select('wechat_id', '微信公众号')
                    ->required()
                    ->options(Wechat::ofAdmin()->ofEnabled()->pluck('name', 'id'))
                    ->help('整个赛季默认使用的公众号，如果场次也做了配置，会覆盖赛季的配置');

                $form->next(function (Form\BlockForm $form) {
                    $form->title('显示配置');

                    $form->column(6, function (Form\BlockForm $form) {
                        $form->switch('config.show_like', '显示点赞')
                            ->width(6, 6)
                            ->default(true);
                        $form->switch('config.show_comment', '显示评论')
                            ->width(6, 6)
                            ->value(true);
                        $form->switch('config.show_support', '显示赞助')
                            ->width(6, 6)
                            ->value(true);
                    });
                    $form->column(6, function (Form\BlockForm $form) {
                        $form->switch('config.show_like_log', '显示点赞记录')
                            ->width(6, 6)
                            ->default(true);
                        $form->switch('config.show_comment_log', '显示评论记录')
                            ->width(6, 6)
                            ->default(true);
                        $form->switch('config.show_support_log', '显示赞助记录')
                            ->width(6, 6)
                            ->default(true);
                    });
                });

                $form->next(function (Form\BlockForm $form) {
                    $form->title('基础配置');
                    $form->number('config.winners', '录取人数')
                        ->default(10)
                        ->width(8, 4)
                        ->required();
                    $form->number('config.rank_lowest_polls', '最低进榜票数')
                        ->default(0)
                        ->width(8, 4)
                        ->required();
                    $form->number('config.like_polls', '点赞增加票数')
                        ->default(1)
                        ->width(8, 4)
                        ->required();
                    $form->number('config.comment_polls', '评论增加票数')
                        ->default(3)
                        ->width(8, 4)
                        ->required();
                    $form->number('config.support_polls', '赞助增加票数')
                        ->default(10)
                        ->help('每一元赞助增加的票数')
                        ->width(8, 4)
                        ->required();
                });

                $form->next(function (Form\BlockForm $form) {
                    $form->title('安全配置');
                    $form->switch('config.show_captcha', '开启验证码')
                        ->width(8, 4)
                        ->value(true);
                    $form->select('config.captcha_type', '验证码配置')
                        ->width(5, 4)
                        ->required()
                        ->options([
                            'default' => '默认配置',
                            'math' => '数学算法',
                            'flat' => '加粗显示',
                            'mini' => 'MINI',
                            'inverse' => '反色显示',
                        ])
                        ->default('default');
                    $form->radio('config.time_check_type', '间隔时间检测方式')
                        ->help('点赞/评论，每自然日可操作 或 每24小时一次')
                        ->width(5, 4)
                        ->required()
                        ->options([
                            'day' => '自然日',
                            '24h' => '24小时',
                        ])
                        ->default('day');
                });
                $form->next(function (Form\BlockForm $form) {
                    $form->title('页面提醒设置');
                    $form->number('config.first_show_before', '关闭排行弹窗')
                        ->default(10)
                        ->width(8, 4)
                        ->help('结束前N分钟,关闭排行榜单弹窗')
                        ->required();
                    $form->number('config.show_rank_time', '排行榜入口')
                        ->default(120)
                        ->width(8, 4)
                        ->help('结束后N分钟,关闭排行榜入口')
                        ->required();
                    $form->switch('config.allow_custom_amount', '允许自定义赞助金额')
                        ->width(8, 4)
                        ->default(true);
                    $form->number('config.amount_min', '最低赞助金额')
                        ->default(1)
                        ->width(8, 4)
                        ->help('最低的赞助金额')
                        ->required();
                    $form->number('config.amount_max', '最高赞助金额')
                        ->default(10000)
                        ->width(8, 4)
                        ->help('单次最高的赞助金额')
                        ->required();
                    $form->number('config.amount_tips', '金额提示')
                        ->default(5000)
                        ->width(8, 4)
                        ->help('赞助金额超过多少，进行提示')
                        ->required();
                });
            });
        });
    }
}
